<?php
namespace app\index\controller;
use \think\Controller;
use \think\View;
class Joke extends Controller
{
	public function index()
	{
		$page = mt_rand(1,50);//随机数
		$showapi_appid = '41567';  //替换此值,在官网的"我的应用"中找到相关值
    	$showapi_secret = 'e71b99da8ab844d68063682ed3237ff6';  //替换此值,在官网的"我的应用"中找到相关值
    	$page = 
    	$paramArr = array(
         	// 'showapi_appid'=> $showapi_appid,
         	// 'page'=> $page,
         	// 'maxResult'=> 1,
         	//添加其他参数
         	'showapi_appid'=> $showapi_appid,
        	'num'=> "",
        	'type'=> "",
        	'yayuntype'=> "",
         	'key'=> ""
    );

    	 $paraStr = "";
         $signStr = "";
         ksort($paramArr);
         foreach ($paramArr as $key => $val) {
             if ($key != '' && $val != '') {
                 $signStr .= $key.$val;
                 $paraStr .= $key.'='.urlencode($val).'&';
             }
         }
         $signStr .= $showapi_secret;//排好序的参数加上secret,进行md5
         $sign = strtolower(md5($signStr));
         $paraStr .= 'showapi_sign='.$sign;//将md5后的值作为参数,便于服务器的效验

        //$param = createParam($paramArr,$showapi_secret);
    	$url = 'http://route.showapi.com/60-27?'.$paraStr; 
    	echo "请求的url:".$url."<br>\r\n";
    	$result = file_get_contents($url);
    	// echo "返回的json数据:<br>\r\n";
    	// print $result.'<br>\r\n';
    	$result = json_decode($result);
    	$title = "";
    	$text = "";
    	foreach ($result as $k => $v) {
    		if($k=='showapi_res_body'){
    			foreach ($v as $key => $v1) {
    				if($key=='contentlist'){
    					$v1 = array($v1);
    					 foreach ($v1 as $key1 => $v2) {
    					 	$v2  =json_encode($v2); //把她转换为json字符串  
							$v2 = json_decode($v2,true); //再把json字符串格式化为数组  
							// //或者直接是:  
							// $v2 = json_decode(json_encode($v2),true);
							echo "标题：".$v2[0]['title'].'<br>';
    					 	//echo "内容：".$v2[0]['text'].'';
    					 	echo "内容：".$v2[0]['img'].'';
    					 }
    					
    				}
    			}
    		}
    		
    	}
	}


}